package com.gugee.common.web.util;

import java.text.DecimalFormat;

/**
 * 格式化数据
 */
public class FormatUtil {
    static DecimalFormat df = new DecimalFormat("######0.0");
    /**
     * 将int值转换成时间字符串
     * el: 61 -> 01:01  , 3601 -> 1:00:01
     * @param timeInt
     * @return
     */
    public static String getTimeStringByInt(int timeInt){
        if (timeInt < 3600){
            //%i表示第i个参数；$0表示0补齐，$不加0时表示补齐空格；$0n表示不足长度不足n时用0补齐，n可以大于9
            return String.format("%1$02d:%2$02d",timeInt / 60, timeInt % 60);
        }else {
            return String.format("%1$d:%2$02d:%3$02d",timeInt / 3600 ,timeInt % 3600 / 60 ,timeInt % 60);
        }
    }

    public static void main(String[] args) {
        System.out.println(getTimeStringByInt(1));
        System.out.println(getTimeStringByInt(61));
        System.out.println(getTimeStringByInt(3601));
        System.out.println(getTimeStringByInt(103601));
        System.out.println(getTimeStringByInt(1003601));
    }

    public static String getNumber(long num) {
        return getString(num, Math.abs(num), df.format(num));
    }
    public static String getNumber(double num) {
        return getString(num, Math.abs(num), df.format(num));
    }
    private static String getString(double num, double abs, String format) {
        return abs >= 1.0e+9
                ? df.format((num / 1.0e+9)) + "B"
                : abs >= 1.0e+6
                ? df.format((num / 1.0e+6)) + "M"
                : abs >= 1.0e+3
                ? df.format((num / 1.0e+3)) + "K"
                : format + "";
    }
}
